/* TODO
 * -let user specify user id in the app
 *  (make similar to the "new im" button by the gears?)
 * -save preferences when they close laim
 */

var twitterID = 24103;		//must be your number id
var refreshTime = 600000;	//recheck twitter every 600 secs (10 mins)
var updatingStatus = 1; 	//updating is off by default
var setAway = 1;		//remain available with status or become away with status
var contextData = 1;

// make menu items global
var cmd1;
var cmd2;
var cmd3;

var timer = shell.serviceManager.basics.timer;
var tlis = new timerListener();

function timerListener() {}
timerListener.prototype.fire = function() {
	updateStatus();
	timer.setForOffset (tlis, "fire", refreshTime); 
}

function updateStatus() {
	if (updatingStatus == 1) {
		var httpRequest = shell.serviceManager.http.request;
		var listener = new httpListener(); 
		var context = new contextObject(contextData);
		httpRequest.get ("http://www.twitter.com/t/status/user_timeline/" + twitterID + "?callback=laimTwitterCallback&count=1", listener, context, 0);
	}
}

function laimTwitterCallback(obj) {
	var session = scene.paramsDictionary.valueForKey("session");
	if (setAway == 1) {
		session.awayMessage = session.createIm(obj[0].text, "text/plain");
	} else {
		session.statusText = obj[0].text;
	}
}

function contextObject(num) {
	this.num = num;
}

function httpListener() {
	this.onResponse = onResponse;
	this.onError = onError;
}

function onResponse(requestor, code, text, header, payload, context) {
	// get the html/xml/json
	if (payload) {
		var streamReader = shell.serviceManager.basics.rawStreamReader;
		streamReader.stream = payload;
		var text = streamReader.readStringTillEndOfStream('');
		eval(text); //call twitterCallback
	}
}

function onError(requestor, code, context) {
	//do nothing
	//set status to "twitter is broken"
}

function commandListener() {}
commandListener.prototype.Exec = function(id, v) {
	var session = scene.paramsDictionary.valueForKey("session");
	if (id == 0) {
	
		//Toggle Updating
		if (updatingStatus == 1) {
			updatingStatus = 0;
			session.awayMessage = session.createIm("", "text/plain");
			session.statusText = "";
			cmd1.text = "Twitter: Turn Updates On"; 
		} else {
			updatingStatus = 1;
			session.awayMessage = session.createIm("", "text/plain");
			timer.setForOffset(tlis, "fire", 1);
			cmd1.text = "Twitter: Turn Updates Off";
		}
		
	} else if (id == 1) {
	
		//Update Now (and only once)
		var putStatusBack = updatingStatus;
		updatingStatus = 1;	//make sure updating is on
		updateStatus();
		updatingStatus = putStatusBack;

	} else if (id == 2) {
		
		//Toggle Away/Available Updates
		if (setAway == 1) {
			setAway = 0;
			session.awayMessage = session.createIm("", "text/plain");
			cmd3.text = "Twitter: Use 'Away Msg' Updates"; 
		} else {
			setAway = 1;
			cmd3.text = "Twitter: Use 'Available' Updates";
		}
		timer.setForOffset(tlis, "fire", 1);
	}
}

function onDestroyed() {
	scene.disconnectObject("session_");
}

function onConstructed() {
	var session = scene.paramsDictionary.valueForKey("session");
	scene.connectObject (session, "session_");
	
	//check twitter status immediately
	updateStatus();

	//build plug-in menu
	cmd1 = scene.paramsDictionary.valueForKey("pluginInfo").addCommand(0);
	if (updatingStatus == 1) {
		cmd1.text = "Twitter: Turn Updates Off"; 
	} else {
		cmd1.text = "Twitter: Turn Updates On"; 
	}
	cmd1.flags = 0;// buddy centric

	cmd2 = scene.paramsDictionary.valueForKey("pluginInfo").addCommand(1);
	cmd2.text = "Twitter: Update Status Now"; 
	cmd2.flags = 0;// buddy centric
	
	cmd3 = scene.paramsDictionary.valueForKey("pluginInfo").addCommand(2);
	if (setAway == 1) {
		cmd3.text = "Twitter: Use 'Available' Updates"; 
	} else {
		cmd3.text = "Twitter: Use 'Away Msg' Updates"; 
	}
	cmd3.flags = 0;// buddy centric
	scene.paramsDictionary.setValueForKey(new commandListener(), "commandTarget");
}